package com.yonyou.pmclouds.safetyrectification.acceptrecord.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.entity.AcceptRecordVO;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.mapper.AcceptRecordMapper;
import com.yonyou.pmclouds.safetyrectification.acceptrecord.service.IAcceptRecordQuery;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author tanghuan
 * @date 2019/10/26
 */
@Service(interfaceClass = IAcceptRecordQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
public class AcceptRecordQueryImpl implements IAcceptRecordQuery {
    @Autowired
    private AcceptRecordMapper mapper;
    @Autowired
    private PictureRemoteService pictureRemoteService;

    @Override
    public AcceptRecordVO getById(String id) throws BusinessException {
        return mapper.getById(id);
    }

    @Override
    public Map<String, AcceptRecordVO> getByPkBill(String pkBill) throws BusinessException {
        // <整改记录，验收记录>
        Map<String, AcceptRecordVO> resultMap = new HashMap<>();
        if (StringUtils.isEmpty(pkBill))
            return resultMap;

        // 查询验收记录
        List<AcceptRecordVO> acceptRecordList = mapper.getByPkBill(pkBill);
        if (CollectionUtils.isEmpty(acceptRecordList))
            return resultMap;

        Set<String> pkSet = new HashSet<>();
        for (AcceptRecordVO recordVO : acceptRecordList) {
            if (recordVO != null)
                pkSet.add(recordVO.getPkAcceptRecord());
        }

        // 查询图片
        Map<String, List<String>> pictureMap = pictureRemoteService.queryPicsByPkEntitys(pkSet.toArray(new String[0]));
        if (pictureMap != null) {
            List<String> pictures;
            for (AcceptRecordVO vo : acceptRecordList) {
                pictures = pictureMap.get(vo.getPkAcceptRecord());
                if (CollectionUtils.isNotEmpty(pictures)) {
                    vo.setPictures(pictures.toArray(new String[0]));
                }
                resultMap.put(vo.getPkRectificationRecord(), vo);
            }
        }

        return resultMap;
    }
}
